%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <signal.h>

extern numLigne ;
extern nbC;
extern argument;
int colTraite=0;
FILE* f;
char *case_tableau;
float valeur_courant=0;
float *valeur_courante_ligne;
char *valeur_courante_titre;
int nb_mot=0;
int affiche=1;
int prem=1;
int k=0;
void yyerror(char *str); 
int yywrap();
int somme_colonne(double x);
int recuperer_chaine(char y);
void ajout_ligne_somme_colonne();
int ligne_titre=1;
float tab_somme_colonne[4];
char expression_utile[4000]="";
%}


%union{char cval[1000];
	int ival;
	float fval;}

%token  <ival> NOMBRE
%token  <cval> MOT
%token  <cval> BF
%token <fval>  REEL
%token  <cval> DEBUT_TABLE
%token  <cval> DEBUT_CENTER
%token  <cval> DEBUT_TABULAR
%token  <cval> FIN_TABULAR
%token  <cval> FIN_CENTER
%token  <cval> FIN_TABLE
%token  <cval> MULTICOLUMN
%token  <cval> DOUBLE_BS
%token  <cval> ETOILE
%token  <cval> VIRGULE
%token  <cval> HLINE
%token  <cval> TABULATION
%token  <cval> ET
%token  <cval> AO
%token  <cval> AF
%token  <cval> COLLSPAN
%token  <cval> SC
%token  <cval> VSPACE
%token  <cval> BS
%token  <cval> DOLLAR
%token  <cval> CAPTION
%type   <cval> Corps tableau ligne_tableau trash Case Legende_texte Texte Legende


%start file
%%
file: trash Corps 
      |/*rien*/
;
Corps:DEBUT_TABULAR TABULATION tableau FIN_TABULAR Legende trash{char expression[4000]="";
			strcat(expression,"<html>\n\t<body topmargin=200>\n\t\t<center>\n\t\t<table border=5 bgcolor=\"#FFCCCC\" width=\"50%\">");
								    strcat(expression,$3);
							            ajout_ligne_somme_colonne();
								    strcat(expression,expression_utile);
								    strcat(expression,"\n\t</table>");
								    strcat(expression,$5);
								    strcat(expression,"\n\t\t</center>\n\t</body>\n</html>");
								    fprintf(f,"%s",expression);
								    
								    fclose(f);}

      |DEBUT_TABULAR TABULATION tableau FIN_TABULAR trash           {char expression[4000]="";
					strcat(expression,"<html>\n\t<body>\n\t\t<center>\n\t\t<table border=5 bgcolor=\"#FFCCCC\" width=\"50%\">");
								    strcat(expression,$3);
								    ajout_ligne_somme_colonne();
								    strcat(expression,expression_utile);
								    strcat(expression,"\n\t</table>");
								    strcat(expression,"\n\t</body>\n</html>");
								    fprintf(f,"%s",expression);
						
							    	    fclose(f);}
;
tableau: HLINE ligne_tableau tableau {char expression[4000]="";
					   strcat(expression,"\n\t\t\t<tr>");
		                           strcat(expression,$2);
		                           strcat(expression,"\n\t\t\t</tr>");
		                           strcat(expression,$3);
					   strcpy($$,expression);
				           affiche=1;
					}
	|HLINE ligne_tableau HLINE  {char expression[4000]="";	
				            strcat(expression,"\n\t\t\t<tr>");
				             strcat(expression,$2);
				             strcat(expression,"\n\t\t\t</tr>");
					     strcpy($$,expression);
					     affiche=1;
				    }
	|HLINE tableau 		    {char expression[4000]="";
		          		    strcat(expression,"\n\t\t\t<tr>\n\t\t\t</tr>");
                                            strcat(expression,$2);
                                            strcpy($$,expression);
					    affiche=1;
					    }
	
	|HLINE                             {
					    strcpy($$,"\n\t\t\t<tr>\n\t\t\t</tr>");
					    affiche=1;
					   }
;

ligne_tableau:Case ET ligne_tableau {char expression[4000]="";
			   			strcat(expression,"\n\t\t\t\t<td ");
						switch(((char*)argument)[colTraite%nbC]){
							case 'r':
									 strcat(expression,"align=right>");
									 break;
							case 'c':
									 strcat(expression,"align=center>");
									 break;
							case 'l':
									strcat(expression,"align=left>");
									 break;
							
						}
                        		    strcat(expression,$1);
					    strcat(expression,"</td>");
					    strcat(expression,$3);
					    strcpy($$,expression);
						colTraite++;}

	      |Case DOUBLE_BS         {char expression[4000]="";
						
			   			strcat(expression,"\n\t\t\t\t<td ");
						switch(((char*)argument)[colTraite%nbC]){
							case 'r':
									 strcat(expression,"align=right>");
									 break;
							case 'c':
									 strcat(expression,"align=center>");
									 break;
							case 'l':
									strcat(expression,"align=left>");
									 break;
							
						}
                        		    strcat(expression,$1);
					    strcat(expression,"</td>");
					    strcat(expression,"\n\t\t\t\t<td align=center>");
					     char total[10]="";
					     if(valeur_courant==0)
			                     {
						if(prem)
				 		{
				  			 strcat(total,"TOTAUX");
						 }
						else
				 		{
				   			strcat(total,"--");
				 		}
			      		     }
		      	      		     else
			      		     {
			        		if(affiche)
						 {
				    			sprintf(total,"%f",valeur_courant);	
				                 }
						else
						{
							strcat(total,"--");
						}
				 	     }
					    printf("total avant : %s \n\n ",total);
		                            strcat(expression,total);
					    printf("total apres : %s  \n\n",total);
					    strcat(expression,"</td>");
					    strcpy($$,expression);
					    valeur_courant=0;
					    k=0;
					    colTraite++;
					    prem=0;
					    }   

	      |ET ligne_tableau         {char expression[4000]="";
		     			strcat(expression,"\n\t\t\t\t<td></td>");
		     			strcat(expression,$2);
		     			strcpy($$,expression);
		     			colTraite++;}

	      |MULTICOLUMN AO NOMBRE AF TABULATION AO Case AF ET ligne_tableau {char expression[4000]="";
			    strcat(expression,"\n\t\t\t\t<td colspan=\"");
                            char nb[3];
			    sprintf(nb,"%d",$3);
			    strcat(expression,nb);
			    strcat(expression,"\"");
			    char * pch;
			    if((pch=strchr($5,'c'))!=NULL){
				strcat(expression," align=center>");
			    }else if((pch=strchr($5,'l'))!=NULL){
				strcat(expression," align=left>");
		   	    }else if((pch=strchr($5,'r'))!=NULL){
				strcat(expression," align=right>");
			    }
			     strcat(expression, $7);
			    strcat(expression,"</td>");
			    strcat(expression,$9);
			    strcpy($$,expression);
			     colTraite+=$3;}
	   |MULTICOLUMN AO NOMBRE AF TABULATION AO Case AF DOUBLE_BS {char expression[4000]="";
			    strcat(expression,"\n\t\t\t\t<td colspan=\"");
                            char nb[3];
			    sprintf(nb,"%d",$3);
			    strcat(expression,nb);
			    strcat(expression,"\"");
			    char * pch;
			    if((pch=strchr($5,'c'))!=NULL){
				strcat(expression," align=center>");
			    }else if((pch=strchr($5,'l'))!=NULL){
				strcat(expression," align=left>");
		   	    }else if((pch=strchr($5,'r'))!=NULL){
				strcat(expression," align=right>");
			    }
			    strcat(expression,$7);
			    strcat(expression,"</td>");
			    strcat(expression,"\n\t\t\t\t<td align=center>");
			    char total[10]="";
    			    if(valeur_courant==0)
			      {
				if(prem)
				 {
				   strcat(total,"TOTAUX");
				 }
				else
				 {
				   strcat(total,"--");
				 }
			      }
		      	      else
			      {
			        if(affiche)
				 {
				    sprintf(total,"%f",valeur_courant);
				 }
			        else
				 {
				   strcat(total,"--");
				 }
			       }
			     printf("total avant : %s \n\n",total);
			    strcat(expression,total);
			    printf("total  apres: %s \n\n ",total);
			    strcat(expression,"</td>");
			    strcpy($$,expression);
			    k=0;
			    valeur_courant=0;
			    colTraite+=$3;
			    prem=0;
			    }
;
Case:Texte              {strcpy($$,$1);}
    |AO BF Texte AF	{char expression[4000]="";
			strcat(expression,"<b>");
			strcat(expression,$3);
			strcat(expression,"</b>");
			strcpy($$,expression);			 
			}
    |AO SC Texte AF	{char expression[4000]="";
			strcat(expression,"<b>");
			strcat(expression,$3);
			strcat(expression,"</b>");
			strcpy($$,expression);			 
			}
;				
Texte:
	 MOT {strcpy($$,$1);
	      if(prem==0)
	      {
	        affiche=0;
	      }
	      nb_mot++;
             }
	|REEL	{sprintf($$,"%f",$1);
		valeur_courant=$1+valeur_courant;
		somme_colonne($1);
		tab_somme_colonne[k]=$1+tab_somme_colonne[k];
		k++;}

	|NOMBRE {sprintf($$,"%d",$1),
		valeur_courant=$1+valeur_courant;
		somme_colonne($1);
		tab_somme_colonne[k]=$1+tab_somme_colonne[k];
		k++;}

	|DOLLAR Texte DOLLAR {strcpy($$,$2);}
	
	|Texte MOT {strcpy($$,$1);
		    strcat($$,$2);
                    if(prem==0)
	            {
	              affiche=0;
	            }
		    nb_mot++;}
	|Texte REEL {strcat($$,$1);
		     char nb[10];
		     sprintf(nb,"%f",$2);
		     strcat($$,nb);
		     valeur_courant=$2+valeur_courant;
		     somme_colonne($2);
		     tab_somme_colonne[k]=$2+tab_somme_colonne[k];
		     k++;}

	|Texte NOMBRE {strcat($$,$1);
		       char nb[10];
		       sprintf(nb,"%d",$2);
		       strcat($$,nb);
		       valeur_courant=$2+valeur_courant;
		       somme_colonne($2);
		       tab_somme_colonne[k]=$2+tab_somme_colonne[k];
		       k++;}
;
Legende_texte :
	Case {strcpy($$,$1);}
	|Case Legende_texte {char expression[4000]="";
				 strcat(expression,$1);
				 strcat(expression,$2);
				 strcpy($$,expression);}
;

Legende:
	CAPTION AO Legende_texte AF { char expression[4000]="";
				       strcat(expression,"\n\t\t<p>");
			               strcat(expression,$3);
				       strcat(expression,"</p>");
				       strcpy($$,expression);} 

;
trash:
	 MOT {}
	|NOMBRE {}
	|REEL {}
	|AO {}
	|AF {}
	|BS {}
	|ETOILE {}
	|VIRGULE {}
	|DOLLAR
	|DOLLAR trash
	|VIRGULE trash {}
	|BS trash {}
	|ETOILE trash {}
	|AO trash {}
	|AF trash {}
	|MOT trash {}
	|NOMBRE trash {}
	|REEL trash {}    
;

%%
extern  FILE * yyin;
int i=0;
int prem_case=1;
int somme_colonne(double x)
{
                valeur_courante_ligne=malloc(sizeof(float));
		if(prem_case)
		{
		 *valeur_courante_ligne=x;
		  prem_case=0;
		  i++;
		  //tab_valeur_numeriques[i-1]=x;
		}
	       else
	       {
		i++;
		valeur_courante_ligne=realloc(valeur_courante_ligne,sizeof(float)*i);
		valeur_courante_ligne[i-1]=x;
		 //tab_valeur_numeriques[i-1]=x;
		}
		free(valeur_courante_ligne);
}

void ajout_ligne_somme_colonne()
{
    int pas_affichage=0;
    printf("------DEBUT--------");
   char bloc[2000]="";
   char bloc_partial[20]="";
   strcat(expression_utile,"\n\t\t\t<tr>");
   float somme=0;
   int j=0; 
   for(j;j<4;j++)
   {
	if(tab_somme_colonne[j]==0)
        {
          pas_affichage=1;
        }
   } 
   j=0;
   if(pas_affichage)
   {
	     for(j;j<4;j++)
	   {
			 char bloc_partial[20]="";
			 strcat(bloc,"\n\t\t\t\t<td align=center>");
			 strcat(bloc,"--");
			 strcat(bloc,"\n\t\t\t\t</td>");
		
	   } 

   }
   else
   {
	   for(j;j<4;j++)
		{
		 char bloc_partial[20]="";
		 strcat(bloc,"\n\t\t\t\t<td align=center>");
		 sprintf(bloc_partial,"%f",tab_somme_colonne[j]);
		 somme=somme+tab_somme_colonne[j];
		 strcat(bloc,bloc_partial);
		 strcat(bloc,"\n\t\t\t\t</td>");	
		} 
   }

    char bloc_partial_somme[20]="";
   // Pour la colonne des totaux
   strcat(bloc,"\n\t\t\t\t<td style=\"color:red\" align=\"center\"><b>");
   if(pas_affichage==0)
   {  
   sprintf(bloc_partial_somme,"%f",somme);
   strcat(bloc,bloc_partial_somme);
   }
   else
   {
    strcat(bloc_partial_somme,"---");
    strcat(bloc,bloc_partial_somme);
   }
   strcat(bloc,"\n\t\t\t\t<b></td>");
   strcat(expression_utile,bloc);
   strcat(expression_utile,"\n\t\t\t</tr>");
   printf("Expression :%s",expression_utile);

   printf("------FIN--------");
}


int yywrap()
{
    return 1 ;
}

void yyerror(char *str)
{
        fprintf(stderr,"Erreur: %s\n",str);
}

int main(int argc, char *argv[])
{
        printf("[CTRL-D pour finir]\n");
	int j=0;
	for(j;j<4;j++)
	{
	  tab_somme_colonne[j]=0;
	}
	if (argc<=1)
	  {fprintf(stderr,"fichier entree a mettre en param");return 0 ;
	  }
	else{
	f=fopen("tableau.html","w");
	yyin=fopen("exemple_String.tex","r");
        yyparse();
	fclose(yyin);
	printf("\n");
        j=0;
	printf("Nombre de colonnes %d \n ",nbC);
		for(j;j<4;j++)
	{
	 	printf("tab:%f \n",tab_somme_colonne[j]);
	}
        printf("Fin d'exécution.\n") ;
	return 0;
}
}


